package mogiksaop.qliphoth.common.blocks;

import net.minecraft.block.BlockLever;
import net.minecraft.block.BlockRedstoneLight;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntityEnderman;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.play.server.SPacketCombatEvent.Event;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.Mirror;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Rotation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.ChunkCache;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.event.world.BlockEvent.EntityPlaceEvent;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.ItemHandlerHelper;

import mogiksaop.qliphoth.client.core.handler.ModelHandler;
import mogiksaop.qliphoth.client.render.IModelRegister;
import mogiksaop.qliphoth.common.core.QliphothCreativeTab;
import mogiksaop.qliphoth.common.lib.LibBlockNames;
import mogiksaop.qliphoth.common.lib.LibMisc;


import java.util.Random;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class BlockOptimalStarterDevice extends BlockLever implements IModelRegister {
	
	//public static final PropertyEnum<BlockOptimalStarterDevice.EnumOrientation> FACING = PropertyEnum.<BlockOptimalStarterDevice.EnumOrientation>create("facing", BlockOptimalStarterDevice.EnumOrientation.class);
    //public static final PropertyBool POWERED = PropertyBool.create("powered");
    protected static final AxisAlignedBB DEVICE_NORTH_AABB = new AxisAlignedBB(0.1875D, 0.0625D, 0.75D, 0.8125D, 0.9375D, 1.0D);
    protected static final AxisAlignedBB DEVICE_SOUTH_AABB = new AxisAlignedBB(0.1875D, 0.0625D, 0.0D, 0.8125D, 0.9375D, 0.25D);
    protected static final AxisAlignedBB DEVICE_WEST_AABB = new AxisAlignedBB(0.75D, 0.0625D, 0.1875D, 1.0D, 0.9375D, 0.8125D);
    protected static final AxisAlignedBB DEVICE_EAST_AABB = new AxisAlignedBB(0.0D, 0.0625D, 0.1875D, 0.25D, 0.9375D, 0.8125D);
    protected static final AxisAlignedBB DEVICE_UP_AABB = new AxisAlignedBB(0.0625D, 0.0D, 0.0625D, 0.9375D, 0.25D, 0.9375D);
    protected static final AxisAlignedBB DEVICE_DOWN_AABB = new AxisAlignedBB(0.0625D, 0.75D, 0.0625D, 0.9375D, 1.0D, 0.9375D);
	
	protected BlockOptimalStarterDevice() {
		super();
		setTranslationKey(LibBlockNames.OPTIMAL_STARTER_DEVICE);
		setRegistryName(new ResourceLocation(LibMisc.MOD_ID, LibBlockNames.OPTIMAL_STARTER_DEVICE));
		setCreativeTab(QliphothCreativeTab.INSTANCE);
		setHardness(5.0F);
		setSoundType(SoundType.METAL);
		//setDefaultState(blockState.getBaseState().withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.NORTH).withProperty(POWERED, Boolean.valueOf(false)));
		//setDefaultState(blockState.getBaseState().withProperty(BotaniaStateProps.ALTAR_VARIANT, AltarVariant.DEFAULT));
	}

	@SideOnly(Side.CLIENT)
	@Override
	public void registerModels() {
		if(Item.getItemFromBlock(this) != Items.AIR)
			ModelHandler.registerBlockToState(this, 0, getDefaultState());
	}

	@Nullable
	@Override
    public AxisAlignedBB getCollisionBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
    {
		return getBoundingBox(state, source, pos);
    }
	
	@Override
    public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
    {
        switch ((BlockOptimalStarterDevice.EnumOrientation)state.getValue(FACING))
        {
            case EAST:
            default:
                return DEVICE_EAST_AABB;
            case WEST:
                return DEVICE_WEST_AABB;
            case SOUTH:
                return DEVICE_SOUTH_AABB;
            case NORTH:
                return DEVICE_NORTH_AABB;
            case UP_X:
            case UP_Z:
                return DEVICE_UP_AABB;
            case DOWN_X:
            case DOWN_Z:
                return DEVICE_DOWN_AABB;
        }
    }
	
	@Override
	public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing par6, float par7, float par8, float par9) {
		//player.setPositionAndUpdate(player.posX, player.posY + 20f, player.posZ);
		if(!world.isRemote) {
			state = state.cycleProperty(POWERED);
			world.setBlockState(pos, state, 3);
			//player.sendMessage(new TextComponentString("hello world! and your position is : " + player.getPosition()));
			//player.playSound(new SoundEvent(new ResourceLocation("entity.endermen.teleport")), 100f, 100f);
			//world.playSound((EntityPlayer)null, pos, SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.BLOCKS, 0.5F, 0.4F / ((new Random().nextFloat()) * 0.4F + 0.8F));
			float f = ((Boolean)state.getValue(POWERED)).booleanValue() ? 0.6F : 0.5F;
            world.playSound((EntityPlayer)null, pos, SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.BLOCKS, 0.3F, f);
            world.notifyNeighborsOfStateChange(pos, this, false);
            EnumFacing enumfacing = ((BlockLever.EnumOrientation)state.getValue(FACING)).getFacing();
            world.notifyNeighborsOfStateChange(pos.offset(enumfacing.getOpposite()), this, false);
			return true;
		}
		return true;
	}
	
    /*protected static boolean canAttachTo(World worldIn, BlockPos p_181090_1_, EnumFacing p_181090_2_)
    {
        return canPlaceBlock(worldIn, p_181090_1_, p_181090_2_);
    }
    
    protected static boolean canPlaceBlock(World worldIn, BlockPos pos, EnumFacing direction)
    {
        BlockPos blockpos = pos.offset(direction.getOpposite());
        IBlockState iblockstate = worldIn.getBlockState(blockpos);
        boolean flag = iblockstate.getBlockFaceShape(worldIn, blockpos, direction) == BlockFaceShape.SOLID;
        Block block = iblockstate.getBlock();

        if (direction == EnumFacing.UP)
        {
            return iblockstate.isTopSolid() || !isExceptionBlockForAttaching(block) && flag;
        }
        else
        {
            return !isExceptBlockForAttachWithPiston(block) && flag;
        }
    }
	
	@Override
    public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
    {
        IBlockState iblockstate = this.getDefaultState().withProperty(POWERED, Boolean.valueOf(false));

        if (canAttachTo(worldIn, pos, facing))
        {
            return iblockstate.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.forFacings(facing, placer.getHorizontalFacing()));
        }
        else
        {
            for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL)
            {
                if (enumfacing != facing && canAttachTo(worldIn, pos, enumfacing))
                {
                    return iblockstate.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.forFacings(enumfacing, placer.getHorizontalFacing()));
                }
            }

            if (worldIn.getBlockState(pos.down()).isTopSolid())
            {
                return iblockstate.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.forFacings(EnumFacing.UP, placer.getHorizontalFacing()));
            }
            else
            {
                return iblockstate;
            }
        }
    }
	
	@Override
	public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
    {
        super.breakBlock(worldIn, pos, state);
    }
	
	@Override
	public boolean isOpaqueCube(IBlockState state) {
		return false;
	}

	@Override
	public boolean isFullCube(IBlockState state) {
		return false;
	}

//	@Override
//	public boolean hasTileEntity(IBlockState state) {
//		return true;
//	}
//	
//	@Override
//	public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos) {
//	    return Block.NULL_AABB; // field_185506_k，代表一个从 (0,0,0) 到 (0,0,0) 的 AxisAlignedBB
//	}
	
	@Override
    public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
    {
        switch ((BlockOptimalStarterDevice.EnumOrientation)state.getValue(FACING))
        {
            case EAST:
            default:
                return DEVICE_EAST_AABB;
            case WEST:
                return DEVICE_WEST_AABB;
            case SOUTH:
                return DEVICE_SOUTH_AABB;
            case NORTH:
                return DEVICE_NORTH_AABB;
            case UP_X:
            case UP_Z:
                return DEVICE_UP_AABB;
            case DOWN_X:
            case DOWN_Z:
                return DEVICE_DOWN_AABB;
        }
    }
	
	@Override
	public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.byMetadata(meta & 7)).withProperty(POWERED, Boolean.valueOf((meta & 8) > 0));
    }

	@Override
    public int getMetaFromState(IBlockState state)
    {
        int i = 0;
        i = i | ((BlockOptimalStarterDevice.EnumOrientation)state.getValue(FACING)).getMetadata();

        if (((Boolean)state.getValue(POWERED)).booleanValue())
        {
            i |= 8;
        }

        return i;
    }
	
	@Override
	public IBlockState withRotation(IBlockState state, Rotation rot)
    {
        switch (rot)
        {
            case CLOCKWISE_180:

                switch ((BlockOptimalStarterDevice.EnumOrientation)state.getValue(FACING))
                {
                    case EAST:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.WEST);
                    case WEST:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.EAST);
                    case SOUTH:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.NORTH);
                    case NORTH:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.SOUTH);
                    default:
                        return state;
                }

            case COUNTERCLOCKWISE_90:

                switch ((BlockOptimalStarterDevice.EnumOrientation)state.getValue(FACING))
                {
                    case EAST:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.NORTH);
                    case WEST:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.SOUTH);
                    case SOUTH:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.EAST);
                    case NORTH:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.WEST);
                    case UP_Z:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.UP_X);
                    case UP_X:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.UP_Z);
                    case DOWN_X:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.DOWN_Z);
                    case DOWN_Z:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.DOWN_X);
                }

            case CLOCKWISE_90:

                switch ((BlockOptimalStarterDevice.EnumOrientation)state.getValue(FACING))
                {
                    case EAST:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.SOUTH);
                    case WEST:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.NORTH);
                    case SOUTH:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.WEST);
                    case NORTH:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.EAST);
                    case UP_Z:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.UP_X);
                    case UP_X:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.UP_Z);
                    case DOWN_X:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.DOWN_Z);
                    case DOWN_Z:
                        return state.withProperty(FACING, BlockOptimalStarterDevice.EnumOrientation.DOWN_X);
                }

            default:
                return state;
        }
    }
	
	@Override
    public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
    {
        return state.withRotation(mirrorIn.toRotation(((BlockOptimalStarterDevice.EnumOrientation)state.getValue(FACING)).getFacing()));
    }

	@Override
    protected BlockStateContainer createBlockState()
    {
        return new BlockStateContainer(this, new IProperty[] {FACING, POWERED});
    }

	@Override
    public BlockFaceShape getBlockFaceShape(IBlockAccess worldIn, IBlockState state, BlockPos pos, EnumFacing face)
    {
        return BlockFaceShape.UNDEFINED;
    }
	
	public static enum EnumOrientation implements IStringSerializable
    {
        DOWN_X(0, "down_x", EnumFacing.DOWN),
        EAST(1, "east", EnumFacing.EAST),
        WEST(2, "west", EnumFacing.WEST),
        SOUTH(3, "south", EnumFacing.SOUTH),
        NORTH(4, "north", EnumFacing.NORTH),
        UP_Z(5, "up_z", EnumFacing.UP),
        UP_X(6, "up_x", EnumFacing.UP),
        DOWN_Z(7, "down_z", EnumFacing.DOWN);

        private static final BlockOptimalStarterDevice.EnumOrientation[] META_LOOKUP = new BlockOptimalStarterDevice.EnumOrientation[values().length];
        private final int meta;
        private final String name;
        private final EnumFacing facing;

        private EnumOrientation(int meta, String name, EnumFacing facing)
        {
            this.meta = meta;
            this.name = name;
            this.facing = facing;
        }

        public int getMetadata()
        {
            return this.meta;
        }

        public EnumFacing getFacing()
        {
            return this.facing;
        }

        public String toString()
        {
            return this.name;
        }

        public static BlockOptimalStarterDevice.EnumOrientation byMetadata(int meta)
        {
            if (meta < 0 || meta >= META_LOOKUP.length)
            {
                meta = 0;
            }

            return META_LOOKUP[meta];
        }

        public static BlockOptimalStarterDevice.EnumOrientation forFacings(EnumFacing clickedSide, EnumFacing entityFacing)
        {
            switch (clickedSide)
            {
                case DOWN:

                    switch (entityFacing.getAxis())
                    {
                        case X:
                            return DOWN_X;
                        case Z:
                            return DOWN_Z;
                        default:
                            throw new IllegalArgumentException("Invalid entityFacing " + entityFacing + " for facing " + clickedSide);
                    }

                case UP:

                    switch (entityFacing.getAxis())
                    {
                        case X:
                            return UP_X;
                        case Z:
                            return UP_Z;
                        default:
                            throw new IllegalArgumentException("Invalid entityFacing " + entityFacing + " for facing " + clickedSide);
                    }

                case NORTH:
                    return NORTH;
                case SOUTH:
                    return SOUTH;
                case WEST:
                    return WEST;
                case EAST:
                    return EAST;
                default:
                    throw new IllegalArgumentException("Invalid facing: " + clickedSide);
            }
        }

        public String getName()
        {
            return this.name;
        }

        static
        {
            for (BlockOptimalStarterDevice.EnumOrientation BlockOptimalStarterDevice$enumorientation : values())
            {
                META_LOOKUP[BlockOptimalStarterDevice$enumorientation.getMetadata()] = BlockOptimalStarterDevice$enumorientation;
            }
        }
    }*/
	

}
